package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 中国邮箱网
 * @author Persh
 *
 */
public class ChinaemailNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.chinaemail.com.cn/blog/content/[0-9]*/.*.html",
		"http://www.chinaemail.com.cn/blog/content/[0-9]*/"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h3").text();
		content=HtmlCleaner.getContentHtml(url ,doc.select("div.body"));
		date=DateUtils.matchDate(doc.select("div.well p.light_gray").text());
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		ChinaemailNewsAnalyse analyse=new ChinaemailNewsAnalyse();
		String url="http://www.chinaemail.com.cn/blog/content/2780/%E8%83%A1%E5%90%AF%E6%81%92%E7%90%86%E4%BA%8B%E9%95%BF%E5%85%A5%E9%80%892013%E5%9B%BD%E9%99%85%E4%BA%92%E8%81%94%E7%BD%91%E5%90%8D%E4%BA%BA%E5%A0%82.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
